
import {patch} from './vdom/patch';
import Watcher from './observer/watcher';

export function lifecycleMixin(Vue){
  Vue.prototype._update = function(vnode){
     const vm = this;
     vm.$el = patch(vm.$el,vnode);//用虚拟节点创建真实节点，替换掉真实的$el
  }
}

export function mountComponent(vm,el){
    const options = vm.$options;
    vm.$el = el;//真实dom元素
    // console.log(options,vm.$el,'option1111');

    callHook(vm,'beforeMount');

    let updateComponent = ()=>{
      //返回的是虚拟dom _update通过虚拟dom 创建真实dom
      vm._update(vm._render());
    }
    new Watcher(vm,updateComponent,()=>{},true);
    callHook(vm,'mounted');
}

export function callHook(vm,hook){
    const handlers = vm.$options[hook]; //[fn,fn,fn]
    if(handlers){ //找到对应的钩子依次执行
      for(let i=0;i<handlers.length;i++){
        handlers[i].call(vm);
      }
    }
}
